package com.example.constellation.mefrag;

import android.app.Dialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;

import com.example.constellation.R;
import com.example.constellation.bean.StarBean;
import com.example.constellation.luckyfrag.LuckBaseAdapter;
import com.example.constellation.utils.AssetsUtils;

import java.util.List;
import java.util.Map;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link MeFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class MeFragment extends Fragment implements View.OnClickListener {



    CircleImageView iconIv;
    TextView nameTv;
    int pos=0;

//保存选择得到位置
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    private Map<String, Bitmap> map;
    private List<StarBean.StarinfoBean> mdatas;
    private SharedPreferences star_pref;

    public MeFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment MeFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static MeFragment newInstance(String param1, String param2) {
        MeFragment fragment = new MeFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //获取activity传递的数据
        Bundle bundle = getArguments();
        StarBean infoBean = (StarBean) bundle.getSerializable("info");
        mdatas=infoBean.getStarinfo();
      star_pref=  getContext().getSharedPreferences("star_pref", Context.MODE_PRIVATE);






    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {



        // Inflate the layout for this fragment
        View view= inflater.inflate(R.layout.fragment_me, container, false);


        iconIv=view.findViewById(R.id.mefrag_iv);
        nameTv=view.findViewById(R.id.mefrag_tv_name);
        //进行初始化设置
        map = AssetsUtils.getContentlogoImgMap();


        //读取共享参数保存的星座名称与logo名称
        String name= star_pref.getString("name","白羊座");
        String logoname=star_pref.getString("logoname","baiyang");




        Bitmap bitmap = map.get(logoname);
        iconIv.setImageBitmap(bitmap);
        nameTv.setText(name);

        iconIv.setOnClickListener(this);



        return view;
    }

    @Override
    public void onClick(View view) {
        switch(view.getId()){
            case R.id.mefrag_iv:
                showDialog();
                break;
        }

    }
    //弹出一个对话框
    private void showDialog() {
        Dialog dialog=new Dialog(getContext());
        View diagview=LayoutInflater.from(getContext()).inflate(R.layout.me_dialog,null);

        dialog.setContentView(diagview);
        dialog.setTitle("请选择您的星座");
        GridView dialogGv=diagview.findViewById(R.id.mefrag_dialog_gv);
        //设置适配器
        LuckBaseAdapter adapter=new LuckBaseAdapter(getContext(),mdatas);
        dialogGv.setAdapter(adapter);
        //设置dialog可以被取消
        dialog.setCancelable(true);
        //设置点击弹出框的外部，可以取消弹出框
        dialog.setCanceledOnTouchOutside(true);


        //设置gridview每一项的点击事件
        dialogGv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                StarBean.StarinfoBean bean =mdatas.get(i);
                String name=bean.getName();
                String logoname=bean.getLogoname();
                nameTv.setText(name);
                Bitmap bitmap=map.get(logoname);
                iconIv.setImageBitmap(bitmap);
                pos=i;
                dialog.cancel();

            }
        });

        dialog.show();
    }
//失去焦点
    @Override
    public void onPause() {
        super.onPause();
        StarBean.StarinfoBean bean=mdatas.get(pos);
        String name = bean.getName();
        String logoname = bean.getLogoname();
        SharedPreferences.Editor editor=star_pref.edit();
        editor.putString("name",name);
        editor.putString("logoname",logoname);
        editor.commit();

    }
}